package com.ruoyi.patients.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.patients.domain.Patients;
import com.ruoyi.patients.service.IPatientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 患者Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/patients/patients")
public class PatientsController extends BaseController {
    @Autowired
    private IPatientsService patientsService;

    /**
     * 查询患者列表
     */
    @PreAuthorize("@ss.hasPermi('patients:patients:list')")
    @GetMapping("/list")
    public TableDataInfo list(Patients patients) {
        startPage();
        List<Patients> list = patientsService.selectPatientsList(patients);
        return getDataTable(list);
    }

    /**
     * 导出患者列表
     */
    @PreAuthorize("@ss.hasPermi('patients:patients:export')")
    @Log(title = "患者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patients patients) {
        List<Patients> list = patientsService.selectPatientsList(patients);
        ExcelUtil<Patients> util = new ExcelUtil<Patients>(Patients.class);
        util.exportExcel(response, list, "患者数据");
    }

    /**
     * 获取患者详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('patients:patients:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(patientsService.selectPatientsById(id));
    }

    /**
     * 新增患者
     */
    @PreAuthorize("@ss.hasPermi('patients:patients:add')")
    @Log(title = "患者", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patients patients) {
        return toAjax(patientsService.insertPatients(patients));
    }

    /**
     * 修改患者
     */
    @PreAuthorize("@ss.hasAnyPermi('patients:patients:edit')")
    @Log(title = "患者", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patients patients) {
        return toAjax(patientsService.updatePatients(patients));
    }

    /**
     * 删除患者
     */
    @PreAuthorize("@ss.hasPermi('patients:patients:remove')")
    @Log(title = "患者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(patientsService.deletePatientsByIds(ids));
    }
}
